﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeStash.Common.Encryption;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Collections.Specialized;

namespace RESTAPI_Test_Client
{
    public static class Utilities
    {
        internal static DataContractJsonSerializer jss;

        //Encrypt using same rules as website see App.Config and CodeStash web site Web.Config
        public static string GetStringForWebSiteCall(string valueToEncrypt)
        {
            if (EncryptionHelper.EncryptionEnabled)
                return EncryptionHelper.GetEncryptedValue(valueToEncrypt);
            else
                return valueToEncrypt;
        }

        internal static T GetValue<T>(Byte[] results) where T : class
        {
            using (MemoryStream ms = new MemoryStream(results))
            {
                jss = new DataContractJsonSerializer(typeof(T));
                return (T)jss.ReadObject(ms);
            }
        }


        internal static void AddValue(NameValueCollection values, string key, object value)
        {
            jss = new DataContractJsonSerializer(value.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                jss.WriteObject(ms, value);
                string json = Encoding.UTF8.GetString(ms.ToArray());
                values.Add(key, json);
            }
        }
    }
}
